package GUI;

import Kernel.Trajet;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;


public class PropositionTrajet extends javax.swing.JFrame {


	private static final long serialVersionUID = 1L;

	/** Crée une instance de PropositionTrajet */
	public PropositionTrajet() {
		initComponents();
		this.setLocationRelativeTo(this.getParent());
	}

	/**
	 * Crée une instance de PropositionTrajet paramétrée
	 * @param List<List<Trajet>> l liste de liste de trajet récupéré avant
	 */
	public PropositionTrajet(List<List<Trajet>> l) {
		initComponents();
		for (int j = 0; j < l.size(); j++) {
			for (int i = 0; i < l.get(j).size(); i++) {
				this.jTable1.setValueAt(l.get(j).get(i), j, 0);
			}
		}
		this.setLocationRelativeTo(this.getParent());
	}

	/**
	 * Crée une instance de PropositionTrajet paramétrée
	 * @param List<List<List<Trajet>>> l, liste de liste de liste de trajet récupéré avant
	 * @param GregorianCalendar k, date correspondant à la date de départ
	 */
	public PropositionTrajet(List<List<List<Trajet>>> l, GregorianCalendar k) {
		initComponents();
		int cmpt = 0;
		for (int j = 0; j < l.size(); j++) {
			verifDates(l.get(j).get(l.get(j).size() - 1), k);
			if (!(l.get(j).get(l.get(j).size() - 1).size() == 0)) {
				this.jTable1.setValueAt("Itinéraire possible", cmpt, 0);
				cmpt++;
				for (int i = 0; i < l.get(j).size(); i++) {
					for (int h = 0; h < l.get(j).get(i).size(); h++) {
						this.jTable1
								.setValueAt(l.get(j).get(i).get(h), cmpt, 0);
						cmpt++;
					}
				}
			}
		}
		this.setLocationRelativeTo(this.getParent());
	}
	/**
	 * Pour comparer la date de départ des trajets avec la date du gregorian calendar
	 * @param l, liste de trajet
	 * @param k, GregorianCalendar
	 */
	private void verifDates(List<Trajet> l, GregorianCalendar k) {
		Iterator<Trajet> it = l.iterator();
		while (it.hasNext()) {
			Trajet t = it.next();
			if (!((t.getDateDepart().get(Calendar.DATE) == k.get(Calendar.DATE))
					&& (t.getDateDepart().get(Calendar.MONTH) == k
							.get(Calendar.MONTH)) && (t.getDateDepart().get(
					Calendar.YEAR) == k.get(Calendar.YEAR))))
				it.remove();
		}
	}

	/**
	 * Initialisation des composants d'AWT.
	 */
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jButton_valider = new javax.swing.JButton();
		jButton_retour = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("Proposition trajet");

		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { "" }, { "" }, { "" }, { "" },
						{ "" }, { "" }, { "" }, { "" }, { "" },
						{ "" }

				}, new String[] { "Trajets" }));
		jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jTable1MouseClicked(evt);
			}
		});
		jScrollPane1.setViewportView(jTable1);

		jButton_valider.setText("Valider");
		jButton_valider.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_validerActionPerformed(evt);
			}
		});

		jButton_retour.setText("Retour");
		jButton_retour.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_retourActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
																.addGroup(
																		layout.createSequentialGroup()
																				.addComponent(
																						jScrollPane1,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						880,
																						Short.MAX_VALUE)
																				.addContainerGap())
																.addGroup(
																		javax.swing.GroupLayout.Alignment.TRAILING,
																		layout.createSequentialGroup()
																				.addComponent(
																						jLabel1)
																				.addGap(381,
																						381,
																						381)))
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addComponent(
																		jButton_retour)
																.addGap(29, 29,
																		29)
																.addComponent(
																		jButton_valider)
																.addGap(27, 27,
																		27)))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1)
								.addGap(31, 31, 31)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										196,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										12, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jButton_valider)
												.addComponent(jButton_retour))
								.addContainerGap()));

		pack();
	}

	private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
		jTable1.getSelectedRow();
		if(jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString().compareTo("Itinéraire possible") == 0 ){
				JOptionPane.showMessageDialog(this,"Erreur : choisissez un trajet valide." , "Erreur : Trajet", JOptionPane.ERROR_MESSAGE);
		}
		if(jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString().compareTo("") == 0){
			JOptionPane.showMessageDialog(this,"Erreur : choisissez un trajet valide." , "Erreur : Trajet", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jButton_retourActionPerformed(java.awt.event.ActionEvent evt) {
		Etape11 e = new Etape11();
		this.setVisible(false);
		e.setVisible(true);		
	    this.dispose();
	}

	private void jButton_validerActionPerformed(java.awt.event.ActionEvent evt) {

		Trajet t = (Trajet) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
		// On affiche différentes fenêtres selon le type de transport
		if (t.getVehicule().getType().compareTo("avion") == 0|| t.getVehicule().getType().compareTo("Autre") == 0) {
			Etape2_Avion e2_A = new Etape2_Avion(t);
			this.setVisible(false);
			e2_A.setVisible(true);
		} else if (t.getVehicule().getType().compareTo("bateau") == 0) {
			Etape2_Bateau e2_B = new Etape2_Bateau(t);
			this.setVisible(false);
			e2_B.setVisible(true);
		} else if (t.getVehicule().getType().toString().compareTo("train") == 0) {
			Etape2_Train e2_T = new Etape2_Train(t);
			this.setVisible(false);
			e2_T.setVisible(true);
		} else if (t.getVehicule().getType().toString().compareTo("bus") == 0) {
			Etape2_Bus e2_T = new Etape2_Bus(t);
			this.setVisible(false);
			e2_T.setVisible(true);
		}

	}

	private javax.swing.JButton jButton_retour;
	private javax.swing.JButton jButton_valider;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;

}
;